﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MoneyWentWhere.DataModel;
using MoneyWentWhere.SimpleTextFile;
using System.IO;

namespace MoneyWentWhere.MwwSimpleTest
{
    [TestFixture]
    public class OldExcelFormatConvert
    {
        const String LiveDataFile = @"C:\Users\Danilo\Documents\My Dropbox\MWW\MwwFinancesDropbox_v2.xlsx";
        const String SecureOutputPath = @"S:\";
        public static String OutputFile { get { return Path.Combine(SecureOutputPath, "MwwExcelUnenc.csv"); } }

        [Test]
        public void ImportAndSaveLiveExcelData()
        {
            MwwDataFile df = ImportLiveExcelData();
            df.WriteFile(OutputFile);
        }


        public static MwwDataFile ImportLiveExcelData()
        {
            if (!Directory.Exists(SecureOutputPath))
            {
                throw new ApplicationException("Output path " + SecureOutputPath + " does not exist. Mount or create the drive.");
            }

            // Get password
            String password = File.ReadAllText(Path.Combine(SecureOutputPath, "LiveData.password"));

            OldExcelFormatConvert efc = new OldExcelFormatConvert();
            return efc.Convert(LiveDataFile, password);
        }

        public MwwDataFile Convert(String filename, String password)
        {
            MwwDataFile df = new MwwDataFile();

            using (MoneyDataSet excelSet = MoneyDataSet.Load(filename, password))
            {
                foreach (MoneyWentWhere.DataModel.Account ea in excelSet.Accounts)
                {
                    MoneyWentWhere.SimpleTextFile.Account a = ConvertAcct(ea);
                    df.Accounts.Add(a);

                    foreach (MoneyWentWhere.DataModel.Transaction et in ea.Transactions)
                    {
                        MoneyWentWhere.SimpleTextFile.Transaction t = ConvertTx(et);
                        a.Transactions.Add(t);
                    }
                }
            }

            df.ReassignTransferIds();

            return df;
        }

        public MoneyWentWhere.SimpleTextFile.Account ConvertAcct(MoneyWentWhere.DataModel.Account ea)
        {
            MoneyWentWhere.SimpleTextFile.Account a = new MoneyWentWhere.SimpleTextFile.Account()
            {
                Currency = ea.Currency.Id,
                Name = ea.Name,
                Group = "[imported]"
            };
            return a;
        }

        public MoneyWentWhere.SimpleTextFile.Transaction ConvertTx(MoneyWentWhere.DataModel.Transaction et)
        {
            switch (et.Kind)
            {
                case TransactionKind.Normal:
                    return new MoneyWentWhere.SimpleTextFile.Transaction()
                    {
                        Date = et.Date,
                        Amount = et.Amount,
                        Comment = et.Comment,
                        Tags = et.Tags.ToString(),
                    };

                case TransactionKind.Reconcile:
                    return new MoneyWentWhere.SimpleTextFile.Reconcile()
                    {
                        Date = et.Date,
                        Amount = et.Amount,
                        Comment = et.Comment,
                        Tags = et.Tags.ToString(),

                        Balance = et.AccountBalance,
                        MachineComment = "# " + et.Settings.ToRepString(),
                    };

                case TransactionKind.Transfer:
                    return new MoneyWentWhere.SimpleTextFile.Transfer()
                    {
                        Date = et.Date,
                        Amount = et.Amount,
                        Comment = et.Comment,
                        Tags = et.Tags.ToString(),

                        Id = et.Guid.ToString(),
                        TargetId = et.TransferToTx.Guid.ToString(),
                        MachineComment = "# " + et.Settings.ToRepString(),
                    };
            }
            throw new ApplicationException("Unknown kind: " + et);
        }

    }
}